﻿
namespace LiteBlog.Components
{
    using System.Collections.Generic;
    using System.Linq;
    using LiteBlog.Common.Entities;
    using LiteBlog.Components.Contracts;
    using LiteBlog.Repositories.Contracts;
    using LiteBlog.Components.Enums;


    public class ArchiveComp : BaseComp<IArchiveRepository>, IArchiveComp
    {
        public ArchiveComp(IArchiveRepository data, ICacheComp cache) : base(data, cache) { }
        
        public IEnumerable<ArchiveMonth> Get()
        {
            var months = _cache.Get<IEnumerable<ArchiveMonth>>(CacheType.Archive);
            if (months == null)
            {
                months = _data.Get();
                _cache.Put(CacheType.Archive, months);
            }

            return months;
        }

        public ArchiveMonth Get(int month, int year)
        {
            return Get().SingleOrDefault(a => a.Month == month && a.Year == year);
        }

    }
}